/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * anjuta-avr-plugin
 * Copyright (C) Lucas van Dijk 2011 <info@return1.net>
 * 
 * anjuta-avr-plugin is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * anjuta-avr-plugin is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "avr-configure-widget.h"

static void avr_configure_widget_class_init (AVRConfigureWidgetClass *klass);
static void avr_configure_widget_init(AVRConfigureWidget *object);
static void avr_configure_widget_finalize (GObject *object);

static gpointer parent_class;

enum
{
	AVR_CHANGED_SIGNAL,
	LAST_SIGNAL
};

static guint avr_signals[LAST_SIGNAL] = { 0 };

GType
avr_configure_widget_get_type(void)
{
	static GType avr_type = 0;

	if(!avr_type)
	{
		const GTypeInfo avr_info =
		{
			sizeof (AVRConfigureWidgetClass),
			NULL, /* base_init */
			NULL, /* base_finalize */
			(GClassInitFunc) avr_configure_widget_class_init,
			NULL, /* class_finalize */
			NULL, /* class_data */
			sizeof (AVRConfigureWidget),
			0,    /* n_preallocs */
			(GInstanceInitFunc) avr_configure_widget_init,
		};

		avr_type = g_type_register_static (GTK_TYPE_FRAME, "AVRConfigureWidget", &avr_info, 0);
	}

	return avr_type;
}

/**
 * Creates a new AVRConfigureWidget
 * 
 * AVRConfigureWidget is a collection of widgets, containing a GtkFrame,
 * two labels, a combobox with an entry and a normal entry. 
 * 
 * This widgets allows the user to enter the target device, and the CPU frequency.
 * 
 * This widget has a signal 'changed', which will be emitted when the target
 * device changes, or the CPU frequency entry changes.
 */
AVRConfigureWidget *
avr_configure_widget_new(void)
{
	return g_object_new(avr_configure_widget_get_type (), NULL);
}

void
avr_configure_widget_class_init (AVRConfigureWidgetClass *klass)
{
	GObjectClass* object_class = G_OBJECT_CLASS (klass);
	parent_class = g_type_class_peek_parent (klass);

	object_class->finalize = avr_configure_widget_finalize;

	avr_signals[AVR_CHANGED_SIGNAL] = g_signal_new ("changed", 
	                                                G_TYPE_FROM_CLASS(klass),
	                                                G_SIGNAL_RUN_LAST,
	                                                G_STRUCT_OFFSET(AVRConfigureWidgetClass, changed),
	                                                NULL,
	                                                NULL,
	                                                g_cclosure_marshal_VOID__VOID,
	                                                G_TYPE_NONE, 0);
}

static void
avr_configure_widget_changed(GtkWidget *widget, gpointer userdata)
{
	g_signal_emit (userdata, avr_signals[AVR_CHANGED_SIGNAL], 0);
}

static void
avr_configure_widget_init (AVRConfigureWidget *object)
{
	gtk_frame_set_label (GTK_FRAME(object), _("AVR Options"));

	/* Initialize widgets */
	GtkWidget *table = gtk_table_new (2, 2, FALSE);
	object->target_device_combo = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new_with_entry ());
	object->cpu_freq_entry = GTK_ENTRY(gtk_entry_new ());

	GtkWidget *target_label = gtk_label_new(_("Target Device:"));
	GtkWidget *freq_label = gtk_label_new(_("CPU Frequency (Hz):"));

	g_object_set(target_label, "xalign", 0.0, NULL);
	g_object_set(freq_label, "xalign", 0.0, NULL);

	/* Add widgets to the table */
	gtk_table_attach (GTK_TABLE(table), target_label, 0, 1, 0, 1, GTK_FILL | GTK_SHRINK, GTK_EXPAND, 3, 3);
	gtk_table_attach (GTK_TABLE(table), freq_label, 0, 1, 1, 2, GTK_FILL | GTK_SHRINK, GTK_EXPAND, 3, 3);
	gtk_table_attach (GTK_TABLE(table), GTK_WIDGET(object->target_device_combo), 
	                  1, 2, 0, 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 3);
	gtk_table_attach (GTK_TABLE(table), GTK_WIDGET(object->cpu_freq_entry), 
	                  1, 2, 1, 2, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 3);
	
	/* Add Table to the frame */
	gtk_container_add(GTK_CONTAINER(object), table);

	g_signal_connect(object->target_device_combo, "changed",
	                 G_CALLBACK(avr_configure_widget_changed),
	                 (gpointer) object);

	g_signal_connect(object->cpu_freq_entry, "changed",
	                 G_CALLBACK(avr_configure_widget_changed),
	                 (gpointer) object);

	gtk_widget_show_all(GTK_WIDGET(object));
}

void
avr_configure_widget_add_target_device (AVRConfigureWidget *obj, const gchar *name)
{
	gtk_combo_box_text_append_text(obj->target_device_combo, name);
}

void
avr_configure_widget_set_target_device (AVRConfigureWidget *obj, gint pos)
{
	gtk_combo_box_set_active (GTK_COMBO_BOX(obj->target_device_combo), pos);
}

void
avr_configure_widget_set_cpu_freq (AVRConfigureWidget *obj, const gchar *freq)
{
	gtk_entry_set_text(obj->cpu_freq_entry, freq);
}

const gchar *
avr_configure_widget_get_cpu_freq (AVRConfigureWidget *obj)
{
	return gtk_entry_get_text (obj->cpu_freq_entry);
}

const gchar *
avr_configure_widget_get_target_device (AVRConfigureWidget *obj)
{
	return gtk_combo_box_text_get_active_text (obj->target_device_combo);
}

static void
avr_configure_widget_finalize (GObject *object)
{
	G_OBJECT_CLASS (parent_class)->finalize (object);
}

